package com.videoaggre.zcl.common

import android.app.Activity
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
import com.alibaba.fastjson.JSON
import com.kingja.loadsir.callback.SuccessCallback
import com.kingja.loadsir.core.LoadService
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.videoaggre.zcl.App
import com.videoaggre.zcl.helper.loadsir.*
import com.videoaggre.zcl.utils.Logger
import java.text.DecimalFormat
import java.text.SimpleDateFormat
import java.util.*

fun CharSequence?.clipboard(successText: String = "复制成功") {
    if (this == null) return
    try {
        val cm = App.app.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        val mClipData = ClipData.newPlainText("Label", this)
        cm.setPrimaryClip(mClipData)
        com.videoaggre.zcl.common.toastMessageLong(successText)
    } catch (e: Exception) {
        Logger.e(e)
    }
}

fun Context?.goActicity(clazz: Class<*>?, bundle: Bundle? = null, requestCode: Int? = null) {
    if (this == null || clazz == null) return
    val intent = Intent(this, clazz)
    if (bundle != null)
        intent.putExtras(bundle)
    if (requestCode != null && this is Activity) {
        startActivityForResult(intent, requestCode)
    } else {
        startActivity(intent)
    }
}

fun Context?.goActicityFinish(clazz: Class<*>, bundle: Bundle? = null, requestCode: Int? = null) {
    goActicity(clazz, bundle, requestCode)
    if (this is Activity)
        finish()
}

fun View.visible() = apply { visibility = View.VISIBLE }
fun View.gone() = apply { visibility = View.GONE }
fun View.invisible() = apply { visibility = View.INVISIBLE }
fun <T> LoadService<T>.showErrorCallback() = kotlin.runCatching { showCallback(ErrorCallback::class.java) }.let { if (it.isFailure) showCallback(ScrollErrorCallback::class.java) }
fun <T> LoadService<T>.showEmptyCallback() = kotlin.runCatching { showCallback(EmptyCallback::class.java) }.let { if (it.isFailure) showCallback(ScrollEmptyCallback::class.java) }
fun <T> LoadService<T>.showLoadingCallback() = showCallback(LoadingCallback::class.java)
fun <T> LoadService<T>.showLoadingBeforeSuccessCallback() {
    if (currentCallback != SuccessCallback::class.java) showCallback(LoadingCallback::class.java)
}

fun RefreshLayout?.stopRefresh() {
    kotlin.runCatching {
        this?.finishRefresh(0)
        this?.finishLoadMore(0)
    }
}

fun CharSequence?.toastMessageLong() = this?.let { toastMessageLong(toString()) }
fun Double.decimalFormat() = DecimalFormat("0.#").format((this))
fun Int.toastMessageLong() = com.videoaggre.zcl.common.toastMessageLong(getString())
fun Int.toColor() = ContextCompat.getColor(App.app, this)
fun Int.getString() = com.videoaggre.zcl.common.getString(this)
fun Int?.dp2px() = this?.let { com.videoaggre.zcl.common.dp2px(this) } ?: 0
fun Any.objectClearNull() = JSON.parseObject(JSON.toJSONString(this))
fun Double?.format() = this?.let { DecimalFormat("0.##").format(this) } ?: ""
fun Date.toYYYYMMDD() = SimpleDateFormat("yyyy-MM-dd").format(this)